package com.dqx.haoqiu.mvp.widget;

import android.content.Context;
import android.graphics.Color;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.TextView;

import com.dqx.haoqiu.mvp.R;
import com.dqx.haoqiu.mvp.utils.Navigation;
import com.xmjs.baselibrary.view.dialog.ExSweetAlertDialog;

/**
 * 常规对话框
 */
public class PrivacyDialog extends ExSweetAlertDialog {

    public PrivacyDialog(Context context) {
        super(context, R.layout.dialog_privacy);
        setCancelable(false);
        setCancelable1(false);
        setCanceledOnTouchOutside(false);
        setCanceledOnTouchOutside1(false);
        setWidth((int) context.getResources().getDimension(R.dimen.dimen_270dp));

        //用户协议和隐私政策
        setTextColorAndClick((TextView) getViewById(R.id.tv_content),
                getContext().getString(R.string.privacy_hint),
                getContext().getResources().getColor(R.color.color_2872fa),
                new int[]{51, 57, 124, 130},
                new PrivacyTextClick(), new PrivacyTextClick());
    }

    public PrivacyDialog setConfirmListener(OnConfirmListener listener) {
        getViewById(R.id.tv_comfirm).setOnClickListener(view -> {
            this.dismiss();
            if (listener != null)
                listener.onConfirm(this);
        });
        return this;
    }

    public interface OnConfirmListener {
        void onConfirm(PrivacyDialog dialog);
    }

    /**
     * 隐私政策点击事件
     */
    private class PrivacyTextClick extends ClickableSpan {

        @Override
        public void onClick(View view) {
            Navigation.openLink(getContext(), getContext().getString(R.string.privacy),
                    getContext().getString(R.string.privacy_file_path));
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            ds.setUnderlineText(false);
        }
    }

    /**
     * 设置字符串部分字体颜色和点击事件
     *
     * @param textView 文本控件
     * @param content  字符串内容
     * @param color    颜色
     * @param position 起止位置
     * @param click    点击事件
     */
    public static void setTextColorAndClick(TextView textView, String content, int color,
                                            int[] position, ClickableSpan... click) {
        SpannableStringBuilder spannableBuilder = new SpannableStringBuilder(content);
        for (int i = 0; i < position.length; i += 2) {
            spannableBuilder.setSpan(new ForegroundColorSpan(color), position[i], position[i + 1], Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            spannableBuilder.setSpan(click[i / 2], position[i], position[i + 1], Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        textView.setMovementMethod(LinkMovementMethod.getInstance());
        textView.setText(spannableBuilder);
        textView.setHighlightColor(Color.TRANSPARENT);
    }
}
